<?php

// +----------------------------------------------------------------------

namespace app\user\home;

use app\index\controller\Home;
use app\user\model\Member as MemberModel;
use app\user\model\Money as MoneyModel;
use app\user\model\Vip as VipModel;
use app\user\model\Area as AreaModel;
use app\user\model\Level as LevelModel;
use app\user\model\Rebate as RebateModel;
use app\user\model\MoneyTake;
use app\api\model\Times;
use qrcode\Qrcode as QrcodeTool;
use think\facade\Db;

/**
 * Vip用户控制器
 * @package app\user\home
 */
class Vip extends Home
{
	/**
	 * Vip二维码
	 * @author paulxun
	 * @return fetch|view
	 */
	public function Qrcode()
	{
		// 判断登录
		$uid = is_login();
		if ($uid <= 0) $this->error('用户登录失效');
		$qrcode = new QrcodeTool;
		$url = real_url('user/member/userinfo', ['pid' => $uid]);
		$qrcode->make($url, 8);
	}
	
	/**
	 * Vip推广页
	 * @author paulxun
	 * @return fetch|view
	 */
	public function Index()
	{
		// 判断登录
		$uid = is_login();
		$info = [
			'qrcode'	=> real_url('user/vip/qrcode'),
			'url'		=> real_url('user/member/userinfo', ['pid' => $uid])
		];
		$this->assign('info', $info);
		$this->assign('active', 'member');
		
		// 获取用户信息
		$user = MemberModel::where('id', $uid)->find();
		$vip = VipModel::where('member_id', $uid)->where('status', 1)->find();
		if ($vip) {
			$user['group'] = LevelModel::level_list($vip['group']);
		}
		$this->assign('user', $user);
		
		// 分享参数
		$share = [
			'title' => '安合元实名认证',
			'desc'	=> 'VIP推广链接，安合元商城实名认证',
			'url'	=> $info['url'],
		];
		$this->assign('share', $share);
		
		return $this->fetch();
	}
	/**
	 * Vip收货地址页
	 * @author paulxun
	 * @return fetch|view
	 */
	public function Address()
	{
		// 判断登录
		$uid = is_login();
		$address = VipModel::getAddress($uid);
		$address['fulladdress'] = $address['province'].'-'.$address['city'].'-'.$address['country'];
		$this->assign('info', $address);
		$this->assign('active', 'member');
		$vip = VipModel::where('member_id', '=', $uid)->find();
		$this->assign('vip', $vip);
		$user['group'] = LevelModel::level_list($vip['group']);
		// 获取用户信息
		$user = MemberModel::where('id', $uid)->find();
		if ($this->request->isPost()) {
			if (!$vip['edit_address']) $this->error('禁止修改，如需修改请联系管理员');
			//获取POST数据
			$data = request()->post();
			$result = $this->validate($data, 'Vip.address');
			if(true !== $result) $this->error($result);
			unset($data['__token__']);
			$data['edit_address'] = 0;
			$update = VipModel::where('member_id', $uid)->update($data);
			if ($update) {
				cache('vip_address_'.$uid, null);
				$this->success('保存成功');
			}
			$this->error('资料更新失败');
		}
		if ($vip) {
			$user['group'] = LevelModel::level_list($vip['group']);
		}
		$this->assign('user', $user);
		
		$this->assign('area', AreaModel::getColumn());
		// 分享参数
		$share = [
			'title' => '安合元商城',
			'desc'	=> '安合元在线商城系统',
			'url'	=> real_url('mall/index/index'),
		];
		$this->assign('share', $share);
		
		return $this->fetch();
	}
	/**
	 * Vip余额页
	 * @author paulxun
	 * @return fetch|view
	 */
	public function Money()
	{
		// 判断登录
		$uid = is_login();
		$this->assign('active', 'member');
		$vip = VipModel::where('member_id', '=', $uid)->find();
		$vip['money'] = $vip['money'] / 100;
		$vip['money_se'] = $vip['money_se'] / 100;
		$this->assign('vip', $vip);
		$map = [
			['uid', '=',  $uid],
		];
		$order = 'create_time desc';	
		$data_list = MoneyModel::getList($map,$order, 6);
		if (request()->isPost()){
			$ajax_list = $data_list->items();
			$ajax = [];
			foreach ($ajax_list as $key => $value) {
				$ajax[$key] = [
					'time'		=> format_time($value['create_time']),
					'type'		=> MoneyModel::type($value['type']),
					'money' 	=> $value['money'] / 100,
					'money_se' 	=> $value['money_se'] / 100,
					'balance' 	=> $value['balance'] / 100,
					'second'	=> $value['second'] / 100,
					'realpay'	=> $value['realpay'] / 100,
				];
			}
			$data['items'] = $ajax;
			$data['last_page'] = $data_list->lastPage();
			$data['total'] = $data_list->total();
			$data['current_page'] = $data_list->currentPage();
			$data['list_rows'] = $data_list->listRows();
			$this->success('加载成功', '', $data);
		}
		// 分享参数
		$share = [
			'title' => '安合元商城',
			'desc'	=> '安合元在线商城系统',
			'url'	=> real_url('mall/index/index'),
			'hide'	=>true,
		];
		$this->assign('share', $share);
		
		return $this->fetch();
	}
	/**
	 * 我的推荐
	 * @author paulxun
	 * @return fetch|view
	 */
	public function users($pid = 0)
	{
		// 判断登录
		$uid = is_login();
		$this->assign('active', 'member');
		$vip = VipModel::where('member_id', '=', $uid)->find();
		$vip['money'] = $vip['money'] / 100;
		$vip['money_se'] = $vip['money_se'] / 100;
		$this->assign('vip', $vip);
		if ($pid == 0) {
			$map = [
				['user_vip.pid', '=',  $uid],
			];
		} else {
			//检查用户
			$uids = VipModel::where('pid', '=', $uid)->column('member_id');
			if (!in_array($pid, $uids)) $this->error('权限不足');
			$map = [
				['user_vip.pid', '=',  $pid],
			];
		}
		$order = 'user_vip.create_time desc';	
		$data_list = VipModel::getList($map,$order, 10);
		if (request()->isPost()){
			$ajax_list = $data_list->items();
			$ajax = [];
			foreach ($ajax_list as $key => $value) {
				$childs = VipModel::getChilds($value['member_id']);
				$ajax[$key] = [
					'id'		=> $value['member_id'],
					'name'		=> $value['realname'],
					'mobile'	=> $value['mobile'],
					'users'		=> count($childs),
					'avatar'	=> $value['avatar_img'],
					'group'		=> LevelModel::level_list($value['group']),
					'shop'		=> LevelModel::shop_list($value['shop']),
				];
			}
			$data['items'] = $ajax;
			$data['last_page'] = $data_list->lastPage();
			$data['total'] = $data_list->total();
			$data['current_page'] = $data_list->currentPage();
			$data['list_rows'] = $data_list->listRows();
			$data['users'] = count(VipModel::getChilds($uid));
			$this->success('加载成功', '', $data);
		}
		// 获取用户信息
		$user = MemberModel::where('id', $uid)->find();
		if ($vip) {
			$user['group'] = LevelModel::level_list($vip['group']);
		}
		$this->assign('user', $user);
		// 分享参数
		$share = [
			'title' => '安合元商城',
			'desc'	=> '安合元在线商城系统',
			'url'	=> real_url('mall/index/index'),
			'hide'	=>true,
		];
		$this->assign('share', $share);
		
		return $this->fetch();
	}
	/**
	 * 我的佣金
	 * @author paulxun
	 * @return fetch|view
	 */
	public function rebate()
	{
		// 判断登录
		$uid = is_login();
		$this->assign('active', 'member');
		$vip = VipModel::where('member_id', '=', $uid)->find();
		$vip['money'] = $vip['money'] / 100;
		$vip['money_se'] = $vip['money_se'] / 100;
		$vip['money_take'] = $vip['money_take'] / 100;
		$map = [
			['user_rebate.uid', '=',  $uid],
		];
		$order = 'user_rebate.create_time desc';	
		$data_list = RebateModel::getList($map,$order, 6);
		if (request()->isPost()){
			$ajax_list = $data_list->items();
			$ajax = [];
			foreach ($ajax_list as $key => $value) {
				$child_info = VipModel::info($value['from_uid']);
				$pluser = '公司直属';
				if ($child_info) {
					$uper = VipModel::info($child_info['pid']); //上级信息
					if ($uper) $pluser = $uper['realname'].'团队';
				}
				$ajax[$key] = [
					'name'		=> $value['realname'],
					'type'		=> RebateModel::type($value['type']),
					'model'		=> RebateModel::model($value['model']),
					'money' 	=> $value['money'] / 100,
					'pay' 		=> $value['pay'] / 100,
					'from'		=> $value['from_uid'] == $uid ? '自己' : $child_info['realname'],
					'money_take'=> $value['money_take'] / 100,
					'time'		=> format_time($value['create_time']),
					'pre'		=> $value['pre'].'%',
					'pluser'	=> $pluser,
				];
			}
			$data['items'] = $ajax;
			$data['last_page'] = $data_list->lastPage();
			$data['total'] = $data_list->total();
			$data['current_page'] = $data_list->currentPage();
			$data['list_rows'] = $data_list->listRows();
			$this->success('加载成功', '', $data);
		}
		// 获取用户信息
		$user = MemberModel::where('id', $uid)->find();
		if ($vip) {
			$user['group'] = LevelModel::level_list($vip['group']);
		}
		
		$count_rebate = cache('rebate_'.$uid);
		
		if (!$count_rebate) {
			// 当月时间区间
			$month = Times::Month();
			$count_rebate['month'] = RebateModel::where('uid', '=', $uid)->where('create_time', 'between', [$month['begin'], $month['end']])->where('model', '<', 3)->sum('money') / 100;
			// 当日时间区间
			$day = Times::Day();
			
			$count_rebate['today'] = RebateModel::where('uid', '=', $uid)->where('create_time', 'between', [$day['begin'], $day['end']])->where('model', '<', 3)->sum('money') / 100;
			
			$count_rebate['all'] = RebateModel::where('uid', '=', $uid)->where('model', '<', 3)->sum('money') / 100;
			cache('rebate_'.$uid, $count_rebate, 600);
		}
		$vip['month'] = $count_rebate['month'];
		$vip['today'] = $count_rebate['today'];
		$vip['all'] = $count_rebate['all'];
		$this->assign('vip', $vip);
		$this->assign('user', $user);
		// 分享参数
		$share = [
			'title' => '安合元商城',
			'desc'	=> '安合元在线商城系统',
			'url'	=> real_url('mall/index/index'),
			'hide'	=>true,
		];
		$this->assign('share', $share);
		
		return $this->fetch();
	}
	
	/**
	 * 佣金提现
	 * @author paulxun
	 * @return fetch|view
	 */
	public function take()
	{
		// 判断登录
		$uid = is_login();
		$this->assign('active', 'member');
		$vip = VipModel::where('member_id', '=', $uid)->find();
		$map = [
			['user_money_take.uid', '=',  $uid],
		];
		$order = 'user_money_take.create_time desc';
		if (request()->isPost()){
			$data_list = MoneyTake::getList($map,$order, 6);
			$data = request()->post();
			if ($vip['status'] == 0 || $vip['group'] < 2) {
				$this->error('暂无权限');
			}
			if (empty($data['apply'])) {
				$ajax_list = $data_list->items();
				$ajax = [];
				foreach ($ajax_list as $key => $value) {
					$ajax[$key] = [
						'id'		=> $value['id'],
						'name'		=> $value['realname'],
						'status_txt'=> MoneyTake::status($value['status']),
						'status'	=> $value['status'],
						'money_take'=> $value['money_take'] / 100,
						'bank'		=> $value['bank'],
						'card'		=> $value['card'],
						'time'		=> format_time($value['create_time']),
					];
				}
				$data['items'] = $ajax;
				$data['last_page'] = $data_list->lastPage();
				$data['total'] = $data_list->total();
				$data['current_page'] = $data_list->currentPage();
				$data['list_rows'] = $data_list->listRows();
				$this->success('加载成功', '', $data);
			} else {
				if ($data['apply'] == 'cancel') {
					if (empty($data['id'])) $this->error('参数错误');
					$apply = MoneyTake::where('id', '=', $data['id'])->where('status', '=', 0)->where('uid', '=', $uid)->find();
					if (!$apply) $this->error('该申请无法取消');
					$apply->delete();
					$this->success('撤销成功');
				} else {
					$apply = MoneyTake::where('status', '<=', 1)->where('uid', '=', $uid)->find();
					if ($apply) $this->error('已有待提现申请，请勿重复提交！');
					$money = trim($data['money']);
					$bank  = trim($data['bank']);
					$card  = trim($data['card']);
					$remark  = trim($data['remark']);
					if (empty($money) || !is_numeric($money) || $money < 100 || $money * 100 > $vip['money_take']) $this->error('提现不能低于100元');
					if ($money * 100 > $vip['money_take']) $this->error('提现不能超过可提现金额');
					$take = new MoneyTake;
					$take->money_take = $money * 100;
					$take->uid = $uid;
					$take->bank = $bank;
					$take->card = $card;
					$take->remark = $remark;
					if ($take->save()){
						if ($vip['bank'] != $bank || $vip['card'] != $card) { //记录银行卡号
							$vip->bank = $bank;
							$vip->card = $card;
							$vip->save();
						}
						$this->success('申请成功');
					} else {
						$this->error('申请失败');
					}
				}
			}
		}
		$vip['money'] = $vip['money'] / 100;
		$vip['money_se'] = $vip['money_se'] / 100;
		$vip['money_take'] = $vip['money_take'] / 100;
		$this->assign('vip', $vip);
		// 获取用户信息
		$user = MemberModel::where('id', $uid)->find();
		if ($vip) {
			$user['group'] = LevelModel::level_list($vip['group']);
		}
		$this->assign('user', $user);
		// 分享参数
		$share = [
			'title' => '安合元商城',
			'desc'	=> '安合元在线商城系统',
			'url'	=> real_url('mall/index/index'),
			'hide'	=>true,
		];
		$this->assign('share', $share);
		
		return $this->fetch();
	}
}
